<html lang="en" xmlns:th="http://www.thymeleaf.org"
	xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head th:replace="layout :: common_header(~{::title},~{},~{})">
    <title>角色管理</title>
</head>
<body>
<div class="easyui-layout" style="width:100%;height:100%;">
<div id="toolbar">
    <div>
        <!--角色管理-保存角色数据-->
        <a sec:authorize="hasAuthority('D1ADCD2E-DDAE-420E-BFC8-E4EE08EAFDB5')" href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-plus" onclick="addMenu()">添加</a>
        <!--角色管理-保存角色数据-->
        <a sec:authorize="hasAuthority('D1ADCD2E-DDAE-420E-BFC8-E4EE08EAFDB5')" href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-pencil" onclick="editMenu()">编辑</a>
        <!--角色管理-删除角色数据-->
        <a sec:authorize="hasAuthority('15E50D59-D883-4D31-9382-F7517F0A16B9')" href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-trash" onclick="removeMenu()">删除</a>
	</div>

    <div>
        <input id="searchbox" class="easyui-textbox" data-options="prompt:'请输入角色名称'">
        <a href="javascript:void(0)" onclick="reloadDatagrid()" class="easyui-linkbutton ListToolStartBtn" data-options="iconCls:'fa fa-search'">查询</a>
        <a href="javascript:void(0)" onclick="RefreshSearch()" class="easyui-linkbutton ListToolEndBtn" data-options="iconCls:'fa fa-refresh'">刷新</a>
    </div>
</div>
<table id="datagrid"></table>
</div>

<th:block th:replace="layout :: scripts"/>
<script th:inline="javascript" type="text/javascript">
    //使用JavaScript脚本控制按钮是否显示
    let canMenuInRole = [[${canMenuInRole}]];
    let canPermissionInRole = [[${canPermissionInRole}]];
    let canUserInRole = [[${canUserInRole}]];
    let canRoleDetail = [[${canRoleDetail}]];

    $(function() {
        InitTreeDataGrid();
    });

    function RefreshSearch() {
        $("#searchbox").searchbox('setValue', '');
        $("#datagrid").datagrid('reload')
    }

    let loadDataUrl = '/Role/LoadRoleList';
    function InitTreeDataGrid() {
        let $dataGrid = $('#datagrid').datagrid({
            url: loadDataUrl,
            method: 'POST',
            idField: 'roleId',
            striped: true,
            fitColumns: true,
            rowNumbers: true,
            singleSelect: true,
            pagination: true,
            nowrap: false,
            fit: true,
            pageSize: 20,
            pageList: [10, 20, 50, 100],
            showFooter: false,
            toolbar: "#toolbar",
            columns: [
                [
                    { field: 'roleId', title: 'ID', width: '15%', align: 'left', hidden: true },
                    { field: 'roleName', title: '角色名', width: '15%', align: 'left',
                        formatter: function (value, row, index) {
                            let roleId = row.roleId;
                            let roleName = row.roleName;
                            let isSystemRole = row.systemRole;
                            let tdContext = roleName;
                            //if (canRoleDetail)
                            tdContext = "<a style='cursor:pointer' onclick=openRolePage('" + roleId + "','detail'," + isSystemRole + ")>" + roleName + "</a>";
                            return tdContext;
                        }
                    },
                    { field: 'displayName', title: '角色名称', width: '15%', align: 'left' },
                    { field: 'isSystemRole', title: '是否为系统角色', width: '10%', align: 'left', formatter: FormatterUtil.BoolFormatter },
                    { field: 'businessTypeString', title: '归属业务', width: 120, align: 'left' },
                    { field: 'description', title: '描述', width: '15%', align: 'left' },
                    { field: 'oper', title: "操作", width: '30%', align: 'left',
                        formatter: function (value, row, index) {
                            let roleId = row.roleId;
                            let isSystemRole = row.systemRole;
                            let tdContext = '<div class="actions">';
                            if (canMenuInRole)
                                tdContext += "<a class='btnContacts' style='cursor:pointer' onclick=openRolePage('" + roleId + "','menu'," + isSystemRole + ")>编辑角色菜单</a>  ";
                            if (canPermissionInRole)
                                tdContext += "<a class='btnContacts' style='cursor:pointer' onclick=openRolePage('" + roleId + "','permission'," + isSystemRole + ")>编辑角色权限</a>  ";
                            if (canUserInRole)
                                tdContext += "<a class='btnContacts' style='cursor:pointer' onclick=openRolePage('" + roleId + "','user'," + isSystemRole + ")>编辑角色用户</a>";
                            return tdContext + '</div>';
                        }
                    }
                ]],
            onBeforeLoad: function (param) {
                param.name=$("#searchbox").searchbox("getValue");
                //param.searchAppliction=$("#searchAppId").combobox('getValue');
            },
            onLoadSuccess: function (data) {
                $(".btnContacts").linkbutton({ iconCls: 'fa fa-user' });
                $dataGrid.datagrid('fixRowHeight')
            },
            onLoadError: function () {
            }
        });
    }
    function searchData(value, name) {
        $('#datagrid').datagrid('unselectAll');
        $('#datagrid').datagrid('load', {
            "name": name
            //"searchAppliction": $('#searchAppId').combobox('getValue')
        });
    }
    function addMenu() {
        openForm("", false,'新增角色');
    }
    function editMenu() {
        let row = $('#datagrid').datagrid('getSelected');
        if (row) {
            if (row.isSystemRole) {
                $.messager.showErrorTopCenter('系统提示', '系统角色无法编辑！', 2000);
                return;
            }
            openForm(row.roleId, true,'编辑角色');
        } else {
            $.messager.showErrorTopCenter('系统提示', '请选择需要编辑的数据', 1000);
        }
    }

    let getFormUrl = '/Role/GetRoleForm';
    let saveFormUrl = '/Role/EditRole';
    function openForm(id, bl,title) {
        let dialog = $.easyui.showDialog({
            title: title,
            width: 400,
            height: 180,
            modal: true,
            topMost: false,
            //enableSaveButton: false,//更改按钮隐藏
            href: getFormUrl + "?id=" + id + "&bl=" + bl,
            enableHeaderContextMenu: false,
            enableApplyButton: false,
            onSave: function(d) {
                let validate = d.form("enableValidation").form("validate");
                if (validate) {
                    $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                    $.ajax({
                        async: true,
                        type: "post",
                        dataType: "json",
                        url: saveFormUrl,
                        data: d.form("getData"),
                        success: function(data) {
                            if (data.success) {
                                d.window('close');
                                if (data.result) {
                                    $('#datagrid').datagrid('clearSelections');
                                    $('#datagrid').datagrid('reload');
                                    $.messager.showInfoTopCenter('系统提示', '保存数据成功。', 1000);
                                } else {
                                    $.messager.showErrorTopCenter('错误消息', '保存数据失败。');
                                }
                            } else {
                                $.messager.showErrorTopCenter('错误消息', data.message);
                            }
                        },
                        complete: function() {
                            $.easyui.loaded();
                        }
                    });
                    return false;
                } else {
                    return false;
                }
            },
            onLoad: function() {
                let f = $(this), ret = $.fn.dialog.defaults.onLoad();
                f.form("disableValidation").form("enableValidation");
                return ret;
            }
        });
    }

    let removeUrl = '/Role/RomoveRole';
    function removeMenu() {
        let row = $('#datagrid').datagrid('getSelected');
        if (row) {
            if (row.isSystemRole) {
                $.messager.showErrorTopCenter('系统提示', '系统角色无法删除', 2000);
                return;
            }
            $.messager.confirm('系统提示', '确认要删除选中行?', function(r) {
                if (r) {
                    $.easyui.loading({ msg: '正在处理，请稍等...' });
                    $.post(removeUrl,
                        { roleId: row.roleId },
                        function(data) {
                            $.easyui.loaded();
                            if (data.success) {
                                console.log(data);
                                if (data.result) {
                                    $('#datagrid').datagrid('clearSelections');
                                    $('#datagrid').datagrid('reload');
                                    $.messager.showInfoTopCenter('系统提示', '删除数据成功。', 1000);
                                } else {
                                    $.messager.showErrorTopCenter('错误消息', '删除数据失败。');
                                }
                            } else {
                                $.messager.showErrorTopCenter('错误消息', '角色下面还分配有相关的用户，请移除关联用户后再重试！');
                            }
                        }, 'json');
                }
            });
        } else {
            $.messager.showErrorTopCenter('系统提示', '请选择需要删除的数据', 1000);
        }
    }

    let menuInRoleUrl = '/Role/MenuInRole';
    let permissionInRoleUrl = '/Role/PermissionInRole';
    let userInRoleUrl = '/Role/UserInRole';
    let roleDetailUrl = '/Role/RoleDetail';
    function openRolePage(id, type, isSystemRole) {
        let url = '';
        let queryString = "roleId=" + id + "&isSystemRole=" + isSystemRole;
        if (type === "menu") {
            url = menuInRoleUrl;
        } else if (type === "permission") {
            url = permissionInRoleUrl;
        } else if (type === "user") {
            url = userInRoleUrl;
        } else if (type === "detail") {
            url = roleDetailUrl;
        }

        MainPage_PostMessage("openSubPage", url, queryString);
    }

    //关闭某个子选项卡
    let closeMenuInRoleUrl = '/Role/MenuInRole';
    function closeMenuInRole() {
        MainPage_PostMessage("closeSubPage", closeMenuInRoleUrl);
    }

    let existRoleNameUrl = '/Role/ExistRoleName';
    function existRoleName(id) {
        $.post(existRoleNameUrl, { id: id, roleName: $("#RoleName").val() }, function (data) {
            if (data) {
                $.messager.showInfoTopCenter('系统提示', '角色名称【' + $("#RoleName").val() + '】已存在,请重新输入。');
                $("#RoleName").val("");
            }
        }, 'json');
    }
</script>
</body>
</html>